﻿using System;
using System.Collections.Generic;
using MugenInjection.Infrastructure;
using MugenInjection.Interface;

namespace MugenInjection.Attributes
{
    /// <summary>
    ///     Attribute that indicates where need inject specified service with specified binding name.
    /// </summary>
    public sealed class NamedParameterAttribute : ResolverAttribute
    {
        #region Constructor

        /// <summary>
        ///     Initializes a new instance of the <see cref="NamedParameterAttribute" /> class.
        /// </summary>
        public NamedParameterAttribute(string name)
        {
            Validate.ArgumentNotNullOrEmpty(name, "name");
            Name = name;
        }

        #endregion

        #region Properties

        /// <summary>
        ///     Gets or sets the binding name.
        /// </summary>
        public string Name { get; set; }

        #endregion

        #region Overrides of ResolverAttribute

        /// <summary>
        ///     Determines whether the specified request can be resolved.
        /// </summary>
        /// <param name="bindingContext">
        ///     The specified <see cref="IBindingContext" />.
        /// </param>
        /// <returns>
        ///     <c>True</c> if the specified service has been resolved; otherwise, <c>false</c>.
        /// </returns>
        public override bool CanResolve(IBindingContext bindingContext)
        {
            IDictionary<string, object> dictionary = InjectorUtilsInternal.GetDictionary(bindingContext);
            SpecialParameterKeys.SetKeyedBindingParameter(dictionary, Name);
            return false;
        }

        /// <summary>
        ///     Resolves instance for the specified <see cref="IBindingContext" />.
        /// </summary>
        /// <param name="bindingContext">
        ///     The specified <see cref="IBindingContext" />.
        /// </param>
        /// <returns>An instance of the service.</returns>
        public override object Resolve(IBindingContext bindingContext)
        {
            throw new NotSupportedException("The NamedParameterAttribute doesn't support the Resolve method.");
        }

        #endregion
    }
}